package com.dushibao.admin.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dushibao.admin.model.Goods;
import com.dushibao.admin.mv.DushibaoModelAndView;
import com.dushibao.admin.service.IGoodsService;
import com.dushibao.admin.vo.request.GoodsModelPageRequest;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.beans.factory.annotation.*;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 * Title: Goods.java
 *
 * Description: 商品 前端控制器
 *
 * Copyright: Copyright (c) 2021
 *
 * Company: www.dushibao.com
 *
 * @author zqd
 *
 * @date 2021-12-10 10:31:58
 *
 * @version dushibao V10.0
 */
@Controller
public class GoodsController {
    
    @Autowired
    IGoodsService goodsService;

    @RequestMapping("/goods/list")
    public ModelAndView list(GoodsModelPageRequest modelPageRequest){
        ModelAndView mv = new DushibaoModelAndView("/goods/list",modelPageRequest);

        QueryWrapper<Goods> queryWrapper = new QueryWrapper<>();
        if(modelPageRequest.getId()!=null){
            queryWrapper.eq("id",modelPageRequest.getId());
        }
        IPage<Goods> page = new Page<>(modelPageRequest.getCurrent(),modelPageRequest.getPageSize());
        IPage<Goods> iPage = this.goodsService.page(page,queryWrapper);

        mv.addObject("iPage",iPage);

        return mv;
    }

    @RequestMapping(value = "/goods/del",method = RequestMethod.POST)
    @ResponseBody
    public Boolean del(@RequestBody List<Long> ids){
        this.goodsService.removeByIds(ids);
        return true;
    }

    @RequestMapping("/goods/add")
    public ModelAndView add(Long id){
        ModelAndView mv = new DushibaoModelAndView("/goods/add");

        if(id == null){
            return mv;
        }

        Goods obj = this.goodsService.getById(id);
        mv.addObject("obj",obj);

        return mv;
    }

    @RequestMapping("/goods/save")
    public String save(Goods goods){
        this.goodsService.saveOrUpdate(goods);
        return "redirect:/goods/list";
    }

    @RequestMapping(value = "/goods/validator",method = RequestMethod.GET)
    @ResponseBody
    public Map<String,Object> validator(Goods goods){
        Map<String,Object> ret = new HashMap<>();

        QueryWrapper<Goods> queryWrapper = new QueryWrapper<>();
        List<Goods> list = this.goodsService.list(queryWrapper);

        if(list == null || list.size() == 0){
            ret.put("code",10000);
            ret.put("msg","验证通过");
        }else{
            ret.put("code",10001);
            ret.put("msg","数据重复");
        }

        return ret;
    }
}
